﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightClient
{
    public partial class RegistrationCW : ChildWindow
    {
        public RegistrationCW()
        {
            InitializeComponent();
        }

        protected override void OnOpened()
        {
            base.OnOpened();
            this.ClearFields();
            tbUsername.Focus();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.enter();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void ClearFields()
        {
            tbUsername.Text = "";
            pbPassword.Password = "";
            pbPasswordConfirm.Password = "";
        }

        private void ChildWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.enter();
            }
            else if (e.Key == Key.Escape)
            {
                this.DialogResult = false;
            }
        }

        private void enter()
        {
            if ((tbUsername.Text == "") || (pbPassword.Password == "") || (pbPasswordConfirm.Password == ""))
            {
                MessageBox.Show("Must enter Username and Password to register.", "Error", MessageBoxButton.OK);
                tbUsername.Focus();
            }
            else if (pbPassword.Password != pbPasswordConfirm.Password)
            {
                MessageBox.Show("Please type again Password and Confirm Password to register.", "Error", MessageBoxButton.OK);
                tbUsername.Focus();
            }
            else
            {
                this.DialogResult = true;
            }
        }
    }
}

